<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
    />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./js/jquery.min.js"></script>
    <script src="./js/crypto-js.min.js"></script>
    <script src="./js/openMusicSDK.js"></script>
    <script src="./js/migu.js"></script>
    <script src="./js/crbtCentralitySDK.js"></script>
  </head>
  <body>
    <div id="logs"></div>
    <script>
      const domLogs = document.getElementById("logs")
      console.log('domLogs', domLogs)
      let logsContent = []
      let params = {};
      function handleLogs(text){
        logsContent.push(text)
        domLogs.innerText = logsContent.join('\n')
      }
      handleLogs('进入页面开始...')

      
      // 监听全局错误
      window.onerror = function(message, source, lineno, colno, error) {
          console.error('捕获到未捕获的错误:', message, error);
          handleLogs(message)
          handleLogs(error)
          // 发送错误信息到服务器
          // 使用 navigator.sendBeacon 在关闭页面前发送数据
          // if (error && error.stack) {
          //     navigator.sendBeacon('/api/log-error', error.stack);
          // }
          return false; // 返回false将错误传递给浏览器默认的错误处理程序
      };

      //查询策略
      function monthPolicybacks() {
        var data = {
          youCallbackName: "monthPolicyback",
          channelCode: params.channelCode,
          token: params.token,
          count: "1",
          type: 5,
          serviceId: params.serviceId,
        };
        handleLogs("查询策略monthPolicybacks开始...")
        queryStrategyBYB(data);
        handleLogs("查询策略monthPolicybacks成功")
      }

      function monthPolicyback(res) {
        handleLogs("查询策略返回: " + JSON.stringify(res))
        console.log("查询策略返回: " + JSON.stringify(res));
        console.log(res);
        if (res.resCode == "000000") {
          submit(res);
        } else {
          document.getElementById("model").innerHTML = res;
          return;
        }
      }
      function submit(res) {
        var data = {
          youCallbackName: "miguOrderSubVrbt_centralityback",
          cpId: res.bizInfoMon.cpId,
          bizCode: res.bizInfoMon.bizCode,
          cpparam: res.bizInfoMon.cpparam,
          salePrice: res.bizInfoMon.salePrice,
          excode: "",
          defSeq: "",
          name: "openCPMB",
          serviceId: params.serviceId,
          channelCode: params.channelCode,
          token: params.token,
        };
        console.log(data);
        handleLogs('提交开始...')
        openCPMB(data);
        handleLogs('提交成功')
      }
      function initParams() {
        handleLogs('获取页面参数开始...')
        const paramUrl = window.location.href.substring(
          window.location.href.indexOf("?") + 1
        );
        const href = paramUrl.replace("data=", "");
        console.log("🚀 ~ init ~ href:", href);
        params = JSON.parse(decodeURIComponent(href));
        console.log("🚀 ~ initParams ~ params:", params);
        handleLogs('获取页面参数成功')
        handleLogs('初始化接口开始...')
        openMiGuInit(
          params.channelCode,
          "monthPolicybacks",
          "paramdialog1",
          "",
          "",
          ""
        );
      }
      function miguOrderSubVrbt_centralityback(res) {
        console.log("开通包月返回: " + JSON.stringify(res));
        handleLogs("开通包月返回: " + JSON.stringify(res))
        document.getElementById("model").innerHTML = JSON.stringify(res);
        if (res.resCode == "000000") {
          // setUp()
        } else {
          document.getElementById("model").innerHTML = JSON.stringify(res);
        }
      }
      
      handleLogs('进入页面成功')
      initParams();
    </script>

    <div id="log"></div>
    <div id="model"></div>
    <div id="model1"></div>
  </body>
</html>
